package com.example.dataservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dataservice.dto.IncomeListDto;
import com.example.dataservice.entity.Income;
import com.example.dataservice.service.IIncomeService;
import com.example.dataservice.utils.Result;
import com.example.dataservice.vo.IncomeCategoryVo;
import com.example.dataservice.vo.IncomeListVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-12
 */
@RestController
@RequestMapping("/api/income")
public class IncomeController {
	@Resource
	IIncomeService incomeService;
	
	private static final String[] CATEGORIES = new String[]{"现金","股票捐赠","银行卡"};
	
	@PostMapping("/list")
	public Result<IncomeListVo> list(@RequestBody IncomeListDto incomeListDto){
		Page<Income> page = new Page<>(incomeListDto.getPage(), incomeListDto.getLimit());
		LambdaQueryWrapper<Income> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(incomeListDto.getCategory()!=null
					&&!incomeListDto.getCategory().equals("")
					&&!incomeListDto.getCategory().equals("请选择"),
			Income::getCategory,incomeListDto.getCategory())
			.eq(incomeListDto.getIncomeDate()!=null,Income::getIncomeDate,incomeListDto.getIncomeDate())
			.eq(incomeListDto.getSource()!=null
				&&!incomeListDto.getSource().equals(""),Income::getSource,incomeListDto.getSource());
		incomeService.page(page,wrapper);
		IncomeListVo incomeListVo = new IncomeListVo()	;
		incomeListVo.setIncomes(page.getRecords());
		incomeListVo.setCount(page.getTotal());
		return Result.success(incomeListVo);
	}
	@PostMapping("/save")
	public Result<String> save(@RequestBody Income income){
		if(income.getId()!=null){
			incomeService.updateById(income);
			return Result.success("更新成功");
		}
		incomeService.save(income);
		return Result.success("保存成功");
	}
	@DeleteMapping("/delete/{id}")
	public Result<String> delete(@PathVariable Long id){
		if(id!=null&&incomeService.getById(id)!=null){
			incomeService.removeById(id);
			return Result.success("删除成功");
		}
		return Result.fail("删除失败");
	}
	@GetMapping("/getIncomes")
	public Result<List<Long>> getIncomes(String year){
		String minDate = year+"-01-01";
		String maxDate = year+"-12-31";
		List<Income> list = incomeService.list(new LambdaQueryWrapper<Income>()
				.ge(Income::getIncomeDate,LocalDate.parse(minDate,DateTimeFormatter.ofPattern("yyyy-MM-dd")))
				.le(Income::getIncomeDate,LocalDate.parse(maxDate,DateTimeFormatter.ofPattern("yyyy-MM-dd"))));
		List<Long> countList = new ArrayList<>();
		for (int i = 1;i<=12;i++){
			long res = 0L;
			for (Income income : list){
				if(income.getIncomeDate().getMonthValue()==i) res+=income.getAmount().intValue();
			}
			countList.add(res);
		}
		return Result.success(countList);
	}
	@GetMapping("/getCategory")
	public Result<List<IncomeCategoryVo>> getCategory(String year){
		String minDate = year+"-01-01";
		String maxDate = year+"-12-31";
		List<Income> list = incomeService.list(new LambdaQueryWrapper<Income>()
			.ge(Income::getIncomeDate, minDate)
			.le(Income::getIncomeDate, maxDate));
		List<IncomeCategoryVo> categoryList = new ArrayList<>();
		for(String category:CATEGORIES){
			long res = 0L;
			for (Income income : list){
				if(income.getCategory().equals(category))res++;
			}
			IncomeCategoryVo incomeCategoryVo = new IncomeCategoryVo(category,res);
			categoryList.add(incomeCategoryVo);
		}
		return Result.success(categoryList);
	}
}

